/**
 * sinmajs - 专为中文开发者设计的JavaScript工具函数集合
 * @version 1.2.1
 * @author sinma<sinma@qq.com>
 * @homepage https://www.sinma.net
 * @license MIT
 */

// 导入各个模块
import * as validator from './modules/validator.js';
import * as string from './modules/string.js';
import * as number from './modules/number.js';
import * as date from './modules/date.js';
import * as array from './modules/array.js';
import * as object from './modules/object.js';
import * as function_ from './modules/function.js';
import * as url from './modules/url.js';
import * as dom from './modules/dom.js';
import * as chinese from './modules/chinese.js';
import * as color from './modules/color.js';
import * as storage from './modules/storage.js';
import * as device from './modules/device.js';
import * as request from './modules/request.js';
import * as crypto from './modules/crypto.js';
import * as json from './modules/json.js';
import * as file from './modules/file.js';
import * as cache from './modules/cache.js';
import * as image from './modules/image.js';
import * as convert from './modules/convert.js';
// 新增 Lodash 风格模块
import * as collection from './modules/collection.js';
import * as lang from './modules/lang.js';
import * as util from './modules/util.js';

// 主要API对象
const sinma = {
  // 版本信息
  version: '1.2.1',

  // 各模块方法
  ...validator,
  ...string,
  ...number,
  ...date,
  ...array,
  ...object,
  ...function_,
  ...url,
  ...dom,
  ...chinese,
  ...color,
  ...storage,
  ...device,
  ...request,
  ...crypto,
  ...json,
  ...file,
  ...cache,
  ...image,
  ...convert,

  // Lodash 风格模块
  ...collection,
  ...lang,
  ...util,

  // 修复方法名冲突
  get: object.get,
  httpGet: request.get,

  // 向后兼容的别名
  aaa: validator.alert,
  isNumber: number.isNumber,
  isNumber2: number.isNumberByRegex,
  isObj: object.isObject,
  isEmpty: validator.isEmpty,
  isPhonenum: validator.isPhone,
  isPhonenum1: validator.isPhoneStrict,
  isTelnum: validator.isTel,
  isTelnum1: validator.isTelStrict,
  isSfz: validator.isIdCard,
  isSfz1: validator.isIdCardStrict,
  GetQueryString: url.getQueryParam,
  GetQueryString2: url.getQueryParamIgnoreCase,
  setOpacity: dom.setOpacity,
  getBase64Image: dom.imageToBase64,
  cutmoney: number.formatMoneySimple, // 万亿单位简化（保持原有别名）
  getname: string.getName,
  getuserpic: string.getUserPic,
  turntime: date.unixToDate,
  turnDate: date.stringToDate,
  turntimemore: date.unixToDateTime,
  qunull: string.removeNull,
  suijishu: string.randomString,
  getnowDatetime: date.getCurrentDateTime,
  getnowDatetime1: date.getDateTimeAfter,
  getnowDate: date.getCurrentDate,
  getoldDate: date.formatDate,
  checklogin: validator.checkLogin,
  tiaomiao: dom.showRealTime,
  calculateAge: date.calculateAge,
  yzmdjs: dom.countdownTimer
};

// 链式调用包装器原型
function ChainWrapper(value) {
  this.value = value;
}

// 添加unwrap方法
ChainWrapper.prototype.unwrap = function() {
  return this.value;
};

// 需要跳过链式包装的方法（这些方法不是"数据优先"参数顺序）
const SKIP_CHAIN_METHODS = new Set([
  'chain',
  'times',     // times(n, iteratee) - n是第一个参数
  'range',     // range(start, end, step) - 不接受数据参数
  'rangeRight',
  'random',    // random(min, max) - 不接受数据参数
  'uniqueId',  // uniqueId(prefix) - 不接受数据参数
  'constant',  // constant(value) - 返回函数
  'identity',  // identity(value) - 单参数函数
  'noop',      // noop() - 无参数函数
  'stubTrue', 'stubFalse', 'stubArray', 'stubObject', 'stubString', // stub函数
  'delay',     // delay(func, wait) - func是第一个参数
  'attempt'    // attempt(func, ...args) - func是第一个参数
]);

// 初始化链式方法（只执行一次）
function initChainMethods() {
  Object.keys(sinma).forEach(key => {
    // 跳过非函数、chain方法本身和不适合链式的方法
    if (typeof sinma[key] !== 'function' || key === 'chain' || SKIP_CHAIN_METHODS.has(key)) {
      return;
    }

    // 在原型上添加链式方法
    ChainWrapper.prototype[key] = function(...args) {
      // 将当前值作为第一个参数传递
      this.value = sinma[key](this.value, ...args);
      return this;
    };
  });
}

// 立即初始化链式方法
initChainMethods();

// 支持链式调用的包装器
sinma.chain = function(value) {
  return new ChainWrapper(value);
};

export default sinma;
